package com.gitee.xmhzzz.component.spring.cloud.component.dao.dto;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gitee.xmhzzz.component.common.enums.PageResult;
import com.gitee.xmhzzz.component.common.utils.BeanUtil;

import java.util.stream.Collectors;


public class PageResultAdapter<T> extends PageResult<T> {

    public static <T> PageResultAdapter<T> adapt(IPage<T> page) {
        PageResultAdapter<T> p = new PageResultAdapter<>();
        p.setNowPage(page.getCurrent());
        p.setPageSize(page.getSize());
        p.setTotalCount(page.getTotal());
        p.setTotalPageNum(page.getPages());
        p.setList(page.getRecords());
        return p;
    }

    public static <T,S> PageResultAdapter<T> adapt(IPage<S> page, Class<T> targetClass) {
        PageResultAdapter<T> p = new PageResultAdapter<>();
        p.setNowPage(page.getCurrent());
        p.setPageSize(page.getSize());
        p.setTotalCount(page.getTotal());
        p.setTotalPageNum(page.getPages());
        p.setList(page.getRecords().stream()
                .map(item -> BeanUtil.convert(item, targetClass)).collect(Collectors.toList()));
        return p;
    }

}
